﻿using MediatR;
using W.Warehouse.Api.Application.Command.BinLocationCommand;
using W.Warehouse.Api.Application.Command.WarehouseTypeCommand;
using W.Warehouse.Api.Application.CommandHandlder.WarehouseTypeCommandHandlder;
using W.Warehouse.Domain;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.BinLocationCommandHandlder
{
    public class BinLocationCreateCommandHandlder : IRequestHandler<BinLocationCreateCommand, ApiResult<int>>
    {
        private readonly ILogger<BinLocationCreateCommandHandlder> _logger;
        private readonly IBinLocationRepository _binLocationRepository;
        public BinLocationCreateCommandHandlder(IBinLocationRepository binLocationRepository, ILogger<BinLocationCreateCommandHandlder> logger)
        {
            _binLocationRepository = binLocationRepository;
            _logger = logger;
        }
        public async Task<ApiResult<int>> Handle(BinLocationCreateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("添加库位管理成功");
                request.CreateTime = DateTime.Now;
                request.IsDelete = false;
                var binLocation = new BinLocation() { BinLocationId = request.BinLocationId, BinLocationNo = request.BinLocationNo, WarehouseId = request.WarehouseId, BinLocationName = request.BinLocationName, WarehouseLocationId = request.WarehouseLocationId, IsDisable = request.IsDisable, IsDefault = request.IsDefault, BinLocationRemarks = request.BinLocationRemarks, office_id = request.office_id, user_id = request.user_id, CreateName = request.CreateName, CreateTime = request.CreateTime, UpdateName = request.UpdateName, UpdateTime = request.UpdateTime, DeleteName = request.DeleteName, DeleteTime = request.DeleteTime, IsDelete = request.IsDelete };
                result.Code = await _binLocationRepository.AddAsync(binLocation);
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                throw;
            }
            return result;
        }
    }
}
